package com.leadbank.baselbf.leadutils;

import android.content.res.Resources;
import android.util.TypedValue;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: LbwADIUtils
 * @Description:
 * @Author: zhouzhiguo
 * @Date: 2024/3/15 17:41
 * @Version: 5.0.6
 * 上海利得金融科技集团版权所有.
 */

public class LbwADIUtils {


    public static boolean isNvl(Object obj) {
        if (null == obj || nvl(obj).equals("") || nvl(obj).equals("null")) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
    public static boolean isNvl(String obj) {
        if (null == obj || getNoNull(obj).equals("") || getNoNull(obj).equals("null")) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /**
     * obj 不为空且不为空格 切部位--
     * @param obj
     * @return
     */
    public static boolean isEmpty(String obj) {
        if (isNvl(obj)) {
            return Boolean.TRUE;
        }
        String result = obj.replaceAll(" ", "");

        if (isNvl(result)) {
            return Boolean.TRUE;
        }

        result = obj.replaceAll("--", "");
        if (isNvl(result)) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    public static boolean isListEmpty(ArrayList obj) {
        if (isNvl(obj)) {
            return Boolean.TRUE;
        }
        if (obj.isEmpty()) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
    public static boolean isListEmpty(List obj) {
        if (isNvl(obj)) {
            return Boolean.TRUE;
        }
        if (obj.isEmpty()) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    // sqlite 中获得的空字符串 有为null 并为 "null"(字符串非空) 的情况 防止
    public static String nvl(Object obj) {
        return obj == null ? "" : (("null").equals(obj.toString()) == true ? ""
                : obj.toString());
    }

    public static String getNoNull(String obj) {
        return obj == null ? "" : (("null").equals(obj) == true ? ""
                : obj);
    }

    public static String nvl(Object obj, String def) {
        return isNvl(obj) ? def : nvl(obj);
    }




    public static int dpToPx(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());
    }


    public static String double2String(Double d) {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(Math.floor(d * 100) * 0.01d);
    }


    /**
     * fragment 上次刷新时间
     */
    private static long lastFragmentRefreshTime = System.currentTimeMillis();

    /**
     * fragment 是否已经刷新
     *
     * @return
     */
    public synchronized static boolean isFragmentRefresh() {
        long time = System.currentTimeMillis();
        if (time - lastFragmentRefreshTime < 500) {
            //防止一直狂按
            lastFragmentRefreshTime = time;
            return true;
        }
        lastFragmentRefreshTime = time;
        return false;
    }
}
